home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Think Class Libraries / WASTE TCL 1.8 / WASTEEdit / CEditApp.cp next >
Encoding:
Text File  |  1995-11-05  |  7.9 KB  |  336 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditApp.c
  3.     
  4.     Application methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include "Commands.h"
  11. #include "CBartender.h"
  12. #include "CEditApp.h"
  13. #include "CEditDoc.h"
  14. #include "Global.h"
  15. #include "WASTE.h"
  16. #ifndef __SCRIPT__
  17. #include <Script.h>
  18. #endif
  19. #include <TextServices.h>
  20. #include <GestaltEqu.h>
  21. #include <FragLoad.h>
  22. #include <Drag.h>
  23.  
  24. #include "CTSMSwitchboard.h"
  25. #include "CTSMDesktop.h"
  26.  
  27. static Boolean    TSMAvailable(void);
  28. static Boolean    DragAndDropAvailable(void);
  29.  
  30. short gUsingTSM = false;
  31. Boolean gHasDragAndDrop = false;
  32.  
  33. extern    OSType        gSignature;
  34. extern    CBartender    *gBartender;
  35. extern    CDesktop     *gDesktop;
  36.  
  37. #define        MENUcolor            135
  38.  
  39. #define        kExtraMasters        10
  40. #define        kRainyDayFund        45000
  41. #define        kCriticalBalance    40000
  42. #define        kToolboxBalance        20000
  43.  
  44.  
  45. /***
  46.  * IEditApp
  47.  *
  48.  *    Initialize the application. Your initialization method should
  49.  *    at least call the inherited method. If your application class
  50.  *    defines its own instance variables or global variables, this
  51.  *    is a good place to initialize them.
  52.  *
  53.  ***/
  54.  
  55. void CEditApp::IEditApp(void)
  56.  
  57. {
  58.         // stuff added for WASTE-TCL
  59.     gUsingTSM = TSMAvailable() && (InitTSMAwareApplication()==noErr);
  60.     gHasDragAndDrop = DragAndDropAvailable();
  61.  
  62.     CApplication::IApplication( kExtraMasters, kRainyDayFund,
  63.                         kCriticalBalance, kToolboxBalance);
  64.  
  65. }
  66.  
  67.  
  68.  
  69. /***
  70.  * SetUpFileParameters
  71.  *
  72.  *    In this routine, you specify the kinds of files your
  73.  *    application opens.
  74.  *
  75.  *
  76.  ***/
  77.  
  78. void CEditApp::SetUpFileParameters(void)
  79.  
  80. {
  81.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  82.  
  83.         /**
  84.          **    sfNumTypes is the number of file types
  85.          **    your application knows about.
  86.          **    sfFileTypes[] is an array of file types.
  87.          **    You can define up to 4 file types in
  88.          **    sfFileTypes[].
  89.          **
  90.          **/
  91.  
  92.     sfNumTypes = 1;
  93.     sfFileTypes[0] = 'TEXT';
  94.  
  95.         /**
  96.          **    Although it's not an instance variable,
  97.          **    this method is a good place to set the
  98.          **    gSignature global variable. Set this global
  99.          **    to your application's signature. You'll use it
  100.          **    to create a file (see CFile::CreateNew()).
  101.          **
  102.          **/
  103.  
  104.     gSignature = '???\?';
  105. }
  106.  
  107.  
  108. /***
  109.  * SetUpMenus
  110.  *
  111.  *    In this method, you add special menu items and set the
  112.  *    menu item dimming and checking options for your menus.
  113.  *    The most common special menu items are the names of the
  114.  *    fonts. For this tiny editor, you also want to set up the
  115.  *    dimming and checking options so only the current font
  116.  *    and size are checked.
  117.  *
  118.  ***/
  119.  
  120. void CEditApp::SetUpMenus(void)
  121.  
  122. {
  123.         /**
  124.          ** Let the default method read the menus from
  125.          **    the MBAR 1 resource.
  126.          **
  127.          **/
  128.  
  129.     inherited::SetUpMenus();
  130.  
  131.         /**
  132.          ** Add the fonts in the  system to the
  133.          **    Font menu. Remember, MENUfont is one
  134.          **    of the reserved font numbers.
  135.          **
  136.          **/
  137.  
  138.     AddResMenu(GetMHandle(MENUfont), 'FONT');
  139.  
  140.         /**
  141.          **    The UpdateMenus() method sets up the dimming
  142.          **    for menu items. By default, the bartender dims
  143.          **    all the menus, and each bureaucrat is reponsible
  144.          **    for turning on the items that correspond to the commands
  145.          **    it can handle.
  146.          **
  147.          **    Set up the options here. The edit pane's UpdateMenus()
  148.          **    method takes care of doing the work.
  149.          **
  150.          **    For Font and Size menus, you want all the items to
  151.          **    be enabled all the time. In other words, you don't
  152.          **    want the bartender to ever dim any of the items
  153.          **    in these two menus.
  154.          **
  155.          **/
  156.  
  157.     gBartender->SetDimOption(MENUfont, dimNONE);
  158.     gBartender->SetDimOption(MENUsize, dimNONE);
  159.     gBartender->SetDimOption(MENUstyle, dimNONE);
  160.     gBartender->SetDimOption(MENUcolor, dimNONE);
  161.  
  162.         /**
  163.          **    For Font and Size menus, one of the items
  164.          **    is always checked. Setting the unchecking option
  165.          **    to TRUE lets the bartender know that it should
  166.          **    uncheck all the menu items because an UpdateMenus()
  167.          **    method will check the right items.
  168.          **    For the Style menu, uncheck all the items and
  169.          **    let the edit pane's UpdateMenus() method check the
  170.          **    appropriate ones.
  171.          **
  172.          **/
  173.  
  174.     gBartender->SetUnchecking(MENUfont, TRUE);
  175.     gBartender->SetUnchecking(MENUsize, TRUE);
  176.     gBartender->SetUnchecking(MENUstyle, TRUE);
  177.     gBartender->SetUnchecking(MENUcolor, TRUE);
  178. }
  179.  
  180.  
  181.  
  182. /***
  183.  * CreateDocument
  184.  *
  185.  *    The user chose New from the File menu.
  186.  *    In this method, you need to create a document and send it
  187.  *    a NewFile() message.
  188.  *
  189.  ***/
  190.  
  191. void CEditApp::CreateDocument()
  192.  
  193. {
  194.     CEditDoc    *theDocument = NULL;
  195.     
  196.     // In the event that creating the document fails,
  197.     // we setup an exception handler here. If any
  198.     // of the methods called within the scope of this
  199.     // TRY block fail, an exception will be raised and
  200.     // control will be transferred to the CATCH block.
  201.     // Here, the CATCH block takes care of disposing
  202.     // of the partially created document.
  203.     
  204.     TRY
  205.     {
  206.         theDocument = new(CEditDoc);
  207.             
  208.             /**
  209.              **    Send your document an initialization
  210.              **    message. The first argument is the
  211.              **    supervisor (the application). The second
  212.              **    argument is TRUE if the document is printable.
  213.              **
  214.              **/
  215.         
  216.         theDocument->IEditDoc(this, TRUE);
  217.     
  218.             /**
  219.              **    Send the document a NewFile() message.
  220.              **    The document will open a window, and
  221.              **    set up the heart of the application.
  222.              **
  223.              **/
  224.         theDocument->NewFile();
  225.     }
  226.     CATCH
  227.     {
  228.         ForgetObject( theDocument);
  229.     }
  230.     ENDTRY;
  231. }
  232.  
  233. /***
  234.  * OpenDocument
  235.  *
  236.  *    The user chose Open… from the File menu.
  237.  *    In this method you need to create a document
  238.  *    and send it an OpenFile() message.
  239.  *
  240.  *    The macSFReply is a good SFReply record that contains
  241.  *    the name and vRefNum of the file the user chose to
  242.  *    open.
  243.  *
  244.  ***/
  245.  
  246. void CEditApp::OpenDocument(SFReply *macSFReply)
  247.  
  248. {
  249.     CEditDoc    *theDocument = NULL;
  250.  
  251.     // In the event that opening the document fails,
  252.     // we setup an exception handler here. If any
  253.     // of the methods called within the scope of this
  254.     // TRY block fail, an exception will be raised and
  255.     // control will be transferred to the CATCH block.
  256.     // Here, the CATCH block takes care of disposing
  257.     // of the partially opened document.
  258.  
  259.     TRY
  260.     {    
  261.         theDocument = new(CEditDoc);
  262.             
  263.             /**
  264.              **    Send your document an initialization
  265.              **    message. The first argument is the
  266.              **    supervisor (the application). The second
  267.              **    argument is TRUE if the document is printable.
  268.              **
  269.              **/
  270.         
  271.         theDocument->IEditDoc(this, TRUE);
  272.     
  273.             /**
  274.              **    Send the document an OpenFile() message.
  275.              **    The document will open a window, open
  276.              **    the file specified in the macSFReply record,
  277.              **    and display it in its window.
  278.              **
  279.              **/
  280.         theDocument->OpenFile(macSFReply);
  281.     }
  282.     CATCH
  283.     {
  284.         ForgetObject( theDocument);
  285.     }
  286.     ENDTRY;
  287. }
  288.  
  289. /*** Destructor added for WASTE support ***/
  290. CEditApp::~CEditApp()
  291. {
  292.         if (gUsingTSM) CloseTSMAwareApplication();
  293. }
  294.  
  295. // make TSMSwitchboard
  296. void CEditApp::MakeSwitchboard(void)
  297. {
  298.     if (gSystem.hasAppleEvents && gUsingTSM)
  299.     {
  300.         FailOSErr(WEInstallTSMHandlers());
  301.     }
  302.     itsSwitchboard = (CSwitchboard *)new CTSMSwitchboard();
  303.     itsSwitchboard->InitAppleEvents(); // needed for TCL 2.0.3
  304. }
  305.  
  306. // make TSMDesktop
  307. void CEditApp::MakeDesktop(void)
  308. {
  309.     gDesktop = (CDesktop *)new CTSMDesktop();
  310. }
  311.  
  312. /******************************************************************************
  313.  TSMAvailable - returns true if the TextService Manager is available
  314.  ******************************************************************************/
  315.  
  316. static Boolean TSMAvailable(void)
  317. {
  318.     long    response;
  319.     
  320.     return (Gestalt(gestaltTSMgrVersion, &response)==noErr);
  321. }
  322.  
  323. /******************************************************************************
  324.  DragAndDropAvailable - returns true if drag and drop is available
  325.  ******************************************************************************/
  326.  
  327. static Boolean DragAndDropAvailable(void)
  328. {
  329.     long response;
  330.  
  331.     if (Gestalt(gestaltDragMgrAttr, &response) != noErr) return false;
  332.     if ((response & (1 << gestaltDragMgrPresent)) == 0) return false;
  333. //    if ((Ptr)InstallTrackingHandler == (Ptr)kUnresolvedSymbolAddress) return false;
  334.     return true;
  335. }
  336.